// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    windowHeight: 500,
    username: 'aaaaaa',
    password: 'aaaaa'
  },
  onLoad() {
    this.setData({
      windowHeight: app.globalData.systemInfo.windowHeight,
    })
  },
  // 获取用户信息
  getUserInfo(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
    })
    const { username, password, userInfo } = this.data
    if (username === '' || password === '') {
      wx.showToast({
        title: '请输入用户名和密码',
        icon: 'none'
      })
    }
    wx.request({
      url: 'http://192.168.2.165:8035/account/token/obtain/',
      method: 'post',
      data: {
        username: username,
        password: password,
        userInfo: JSON.stringify(userInfo)
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        wx.setStorageSync('token', res.data.data.token)
        if (res.data.status) {
          app.globalData.user_id = res.data.data.user_id
          wx.navigateTo({
            url: '../chat/chat',
          })
        }
      }
    })
  },
  usernameInput(res) {
    this.setData({
      username: res.detail.value
    })
  },
  passwordInput(res) {
    this.setData({
      password: res.detail.value
    })
  }
})
